package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallPerforAnalysis;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 业绩分析Action
 * @author kangyj
 * @datetime 2014年12月7日,下午3:53:04
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class PerformanceAnalysisAction extends PublicAction implements
		ModelDriven<MedicalCallPerforAnalysis> {

	private static final long serialVersionUID = -6709804184703824400L;
	
	private MedicalCallPerforAnalysis perforAnalysis = new MedicalCallPerforAnalysis();
	
	/**
	 * @desc 员工业绩分析页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月7日,下午4:04:58
	 */
	@Action(value = "per_ana_staff_init", results = {@Result(location = "per_ana_staff_init.jsp")})
	public String staffInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 员工业绩分析查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月9日,上午12:11:48
	 */
	@Action(value = "per_ana_staff_view")
	public String staffView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerAnalysisManagerService().findStaffperforAnalysis(perforAnalysis, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 小组业绩分析页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月7日,下午4:45:17
	 */
	@Action(value = "per_ana_group_init", results = {@Result(location = "per_ana_group_init.jsp")})
	public String groupInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 组业绩分析查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月9日,上午12:12:22
	 */
	@Action(value = "per_ana_group_view")
	public String groupView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerAnalysisManagerService().findGroupPerforAnalysis(perforAnalysis, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 中心业绩分析页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月7日,下午4:46:36
	 */
	@Action(value = "per_ana_center_init", results = {@Result(location = "per_ana_center_init.jsp")})
	public String centerInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 中心业绩分析查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月9日,上午12:12:22
	 */
	@Action(value = "per_ana_center_view")
	public String centerView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerAnalysisManagerService().findCenterPerforAnalysis(perforAnalysis, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 员工业绩导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月13日,下午7:51:15
	 */
	@Action(value = "per_ana_staff_export")
	public String stafEexport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"员工业绩分析.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getPerAnalysisManagerService().exportStaffperforAnalysis(admin, this.perforAnalysis);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 组业绩分析导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月13日,下午11:16:24
	 */
	@Action(value = "per_ana_group_export")
	public String groupExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"组业绩分析.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getPerAnalysisManagerService().exportGroupPerforAnalysis(admin, this.perforAnalysis);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 中心业绩分析导出
	 * @return 
	 * @author liy
	 * @datetime 2014年12月14日,上午12:04:23
	 */
	@Action(value = "per_ana_center_export")
	public String centerExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"话务中心业绩分析.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getPerAnalysisManagerService().exportCenterPerforAnalysis(admin, this.perforAnalysis);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalCallPerforAnalysis getModel() {
		return perforAnalysis;
	}

	
}
